package com.qingfeng.client.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qingfeng.client.VO.AfterSellApplyVo;
import com.qingfeng.client.bean.Activity;
import com.qingfeng.client.bean.Buyer;
import com.qingfeng.client.bean.Goods;
import com.qingfeng.client.bean.GoodsCommon;
import com.qingfeng.client.dao.GoodsCommonMapper;
import com.qingfeng.client.dao.GoodsMapper;
import com.qingfeng.client.service.ActivityService;
import com.qingfeng.client.service.AfterSellService;
import com.qingfeng.util.ApiMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 临时第三方活动使用的相关接口
 * @author zhangyuefei
 * @version 1.0
 * @date 2021/1/19 下午7:53
 */
@Slf4j
@Api(tags = "活动")
@RestController
@RequestMapping("/activity")
public class ActivityApi extends BaseV1Api{

    @Autowired
    private ActivityService service;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    @ApiOperation("检查code是否被使用")
    @PostMapping("/checkCode")
    public ApiMsg checkCode(@RequestBody String reqStr){
        log.info("检查code是否被使用"+reqStr);
        JSONObject json = JSON.parseObject(reqStr);
        String code = json.getString("code");
        String goodsCommonId = json.getString("goodsCommonId");
        GoodsCommon goodsCommon = goodsCommonMapper.selectById(goodsCommonId);
        int result = 0;
        if (goodsCommon.getTypeId()==314){
            result+=1;
            if(service.checkCode(code,goodsCommonId)){
                result+=1;
            }
        }
        return success(result);
    }

    @ApiOperation("手动增加code")
    @PostMapping("/addCode")
    public ApiMsg addCode(@RequestBody String reqStr){
        log.info("检查code是否被使用"+reqStr);
        JSONObject json = JSON.parseObject(reqStr);
        Integer num = json.getInteger("num");
        if (num==null) num=1;
        List<String> codes = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String code = UUID.randomUUID().toString();
            String goodsId = json.getString("goodsId");
            service.addCode(code,goodsId);
            codes.add(code);
        }
        return success("增加完成",codes);
    }
}
